﻿using System;
using TSites.GUI.Enums;
using TSites.Service.Common.Utils;
using System.Windows.Forms;
using System.Configuration;
using System.Globalization;

namespace TSites.GUI
{
	public class ConfigurationSingleton
	{
		public static readonly ConfigurationSingleton Instance = new ConfigurationSingleton();

		private ConfigurationSingleton()
		{
			Read();
		}

		#region Fields

		// Immanuel information
		public Immanuelnfo Immanuel { get; set; }

		// Language
		public LANGUAGE Language { get; set; }

		// Path
		public string XmlPath { get; set; }
		public string WordTemplateFolder { get; set; }
		public string ExportPath { get; set; }

		// Printer name
		public string DefaultPrinterName { get; set; }
		public string UserManualPath { get; set; }

		#endregion

		#region Methods

		private void Read()
		{
			// Immanuel information
			Immanuel = new Immanuelnfo
			{
				VNName = AppConfig.GetString("ImmaVNName", string.Empty),
				ENName = AppConfig.GetString("ImmaENName", string.Empty),
				RepresentedBy = AppConfig.GetString("ImmaRepresentedBy", string.Empty),
				VNPosition = AppConfig.GetString("ImmaVNPosition", string.Empty),
				ENPosition = AppConfig.GetString("ImmaENPosition", string.Empty),
				AuthorizationNo = AppConfig.GetString("ImmaAuthorizationNo", string.Empty),
				TelephoneNo = AppConfig.GetString("ImmaTelephoneNo", string.Empty),
				Fax = AppConfig.GetString("ImmaFax", string.Empty),
				VNAddress = AppConfig.GetString("ImmaVNAddress", string.Empty),
				ENAddress = AppConfig.GetString("ImmaENAddress", string.Empty),
				Beneficiary = AppConfig.GetString("ImmaBeneficiary", string.Empty),
				BankAccount = AppConfig.GetString("ImmaBankAccount", string.Empty),
				BankVNAccountAt = AppConfig.GetString("ImmaVNBankAccountAt", string.Empty),
				BankENAccountAt = AppConfig.GetString("ImmaENBankAccountAt", string.Empty),
				TaxCode = AppConfig.GetString("ImmaTaxCode", string.Empty)
			};
			DateTime temp;
			DateTime.TryParseExact(AppConfig.GetString("ImmaAuthorizationDate", string.Empty),
				GlobalConst.EN_FORMAT_DATE, null, DateTimeStyles.None, out temp);
			Immanuel.AuthorizationDate = temp;

			// Language
			Language = (LANGUAGE)AppConfig.GetInt("Language", 0);

			// Path
			XmlPath = AppConfig.GetCustomString("ConsistentSection", "XmlPath");
			WordTemplateFolder = AppConfig.GetCustomString("ConsistentSection", "WordTemplateFolder");
			ExportPath = AppConfig.GetString("ExportPath", string.Empty);

			// Printer name
			DefaultPrinterName = AppConfig.GetString("DefaultPrinterName", string.Empty);
			UserManualPath = AppConfig.GetCustomString("ConsistentSection", "UserManualPath");
		}

		public bool SaveImmanuelInfo(Immanuelnfo immanuel)
		{
			// Open App.Config of executable
			Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

			bool isChanged = false;
			if (Immanuel.VNName != immanuel.VNName)
			{
				config.AppSettings.Settings.Remove("ImmaVNName");
				config.AppSettings.Settings.Add("ImmaVNName", immanuel.VNName);
				isChanged = true;
			}

			if (Immanuel.ENName != immanuel.ENName)
			{
				config.AppSettings.Settings.Remove("ImmaENName");
				config.AppSettings.Settings.Add("ImmaENName", immanuel.ENName);
				isChanged = true;
			}

			if (Immanuel.RepresentedBy != immanuel.RepresentedBy)
			{
				config.AppSettings.Settings.Remove("ImmaRepresentedBy");
				config.AppSettings.Settings.Add("ImmaRepresentedBy", immanuel.RepresentedBy);
				isChanged = true;
			}

			if (Immanuel.VNPosition != immanuel.VNPosition)
			{
				config.AppSettings.Settings.Remove("ImmaVNPosition");
				config.AppSettings.Settings.Add("ImmaVNPosition", immanuel.VNPosition);
				isChanged = true;
			}

			if (Immanuel.ENPosition != immanuel.ENPosition)
			{
				config.AppSettings.Settings.Remove("ImmaENPosition");
				config.AppSettings.Settings.Add("ImmaENPosition", immanuel.ENPosition);
				isChanged = true;
			}

			if (Immanuel.AuthorizationNo != immanuel.AuthorizationNo)
			{
				config.AppSettings.Settings.Remove("ImmaAuthorizationNo");
				config.AppSettings.Settings.Add("ImmaAuthorizationNo", immanuel.AuthorizationNo);
				isChanged = true;
			}

			if (Immanuel.AuthorizationDate.Date != immanuel.AuthorizationDate.Date)
			{
				config.AppSettings.Settings.Remove("ImmaAuthorizationDate");
				config.AppSettings.Settings.Add("ImmaAuthorizationDate", immanuel.AuthorizationDate.ToString(GlobalConst.EN_FORMAT_DATE));
				isChanged = true;
			}

			if (Immanuel.TelephoneNo != immanuel.TelephoneNo)
			{
				config.AppSettings.Settings.Remove("ImmaVNTelephoneNo");
				config.AppSettings.Settings.Add("ImmaVNTelephoneNo", immanuel.TelephoneNo);
				isChanged = true;
			}

			if (Immanuel.Fax != immanuel.Fax)
			{
				config.AppSettings.Settings.Remove("ImmaVNFax");
				config.AppSettings.Settings.Add("ImmaVNFax", immanuel.Fax);
				isChanged = true;
			}

			if (Immanuel.VNAddress != immanuel.VNAddress)
			{
				config.AppSettings.Settings.Remove("ImmaVNAddress");
				config.AppSettings.Settings.Add("ImmaVNAddress", immanuel.VNAddress);
				isChanged = true;
			}

			if (Immanuel.ENAddress != immanuel.ENAddress)
			{
				config.AppSettings.Settings.Remove("ImmaENAddress");
				config.AppSettings.Settings.Add("ImmaENAddress", immanuel.ENAddress);
				isChanged = true;
			}

			if (Immanuel.Beneficiary != immanuel.Beneficiary)
			{
				config.AppSettings.Settings.Remove("ImmaBeneficiary");
				config.AppSettings.Settings.Add("ImmaBeneficiary", immanuel.Beneficiary);
				isChanged = true;
			}

			if (Immanuel.BankAccount != immanuel.BankAccount)
			{
				config.AppSettings.Settings.Remove("ImmaBankAccount");
				config.AppSettings.Settings.Add("ImmaBankAccount", immanuel.BankAccount);
				isChanged = true;
			}

			if (Immanuel.BankVNAccountAt != immanuel.BankVNAccountAt)
			{
				config.AppSettings.Settings.Remove("ImmaVNBankAccountAt");
				config.AppSettings.Settings.Add("ImmaVNBankAccountAt", immanuel.BankVNAccountAt);
				isChanged = true;
			}

			if (Immanuel.BankENAccountAt != immanuel.BankENAccountAt)
			{
				config.AppSettings.Settings.Remove("ImmaENBankAccountAt");
				config.AppSettings.Settings.Add("ImmaENBankAccountAt", immanuel.BankENAccountAt);
				isChanged = true;
			}

			if (Immanuel.TaxCode != immanuel.TaxCode)
			{
				config.AppSettings.Settings.Remove("ImmaTaxCode");
				config.AppSettings.Settings.Add("ImmaTaxCode", immanuel.TaxCode);
				isChanged = true;
			}

			if (isChanged)
			{
				// Save the changes in App.config file.
				config.Save(ConfigurationSaveMode.Modified);

				// Force a reload of a changed section.
				ConfigurationManager.RefreshSection("appSettings");

				// assign to the new Immanuel information
				Immanuel = immanuel;
			}

			return isChanged;
		}

		public bool SaveLanguage(LANGUAGE language)
		{
			if (Language != language)
			{
				AppConfig.Save("Language", ((int)language).ToString());
				Language = language;
				return true;
			}

			return false;
		}

		public bool SaveExportOutputPath(string path)
		{
			if (ExportPath != path)
			{
				AppConfig.Save("ExportPath", path);
				ExportPath = path;
				return true;
			}

			return false;
		}

		public bool SavePrinterName(string printerName)
		{
			if (DefaultPrinterName != printerName)
			{
				AppConfig.Save("DefaultPrinterName", printerName);
				DefaultPrinterName = printerName;
				return true;
			}

			return false;
		}

		#endregion
	}
}
